package ar.com.spsolutions.splilabrary.view.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;

import ar.com.spsolutions.splibrary.entity.Entity;
import ar.com.spsolutions.splibrary.services.dto.AbstractCRUDDTO;
import ar.com.spsolutions.splilabrary.view.components.modalwindow.SelectModalWindow;

public class Footer<E extends Entity> extends Panel {

    private static final long serialVersionUID = -563283609028903271L;

    private SelectModalWindow feedbackModalWindow;

    public Footer(final String footerId, final AbstractCRUDDTO<E> abstractCRUDDTO, final CRUDComponent<E> crudComponent) {
        super(footerId);
        feedbackModalWindow = new SelectModalWindow("feedbackLoan");
        this.add(feedbackModalWindow);
        final Form<AbstractCRUDDTO<E>> form = crudComponent.getMainForm();
        this.addAcceptButton(form, abstractCRUDDTO, crudComponent);
        this.addCancelButton(form, abstractCRUDDTO, crudComponent);
        setOutputMarkupId(true);
        setOutputMarkupPlaceholderTag(true);
        form.add(this);
    }

    private void addCancelButton(final Form<AbstractCRUDDTO<E>> form, final AbstractCRUDDTO<E> abstractCRUDDTO,
            final CRUDComponent<E> crudComponent) {
        final AjaxSubmitLink ajaxSubmitLink = new AjaxSubmitLink("cancelButton", form) {

            private static final long serialVersionUID = 8292048320258538553L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                abstractCRUDDTO.reset();
                crudComponent.hideForm(target);
            }

            @Override
            protected void onError(final AjaxRequestTarget target, final Form<?> form) {
                crudComponent.hideForm(target);
            }
        };
        ajaxSubmitLink.setOutputMarkupId(true);
        ajaxSubmitLink.setOutputMarkupPlaceholderTag(true);
        this.add(ajaxSubmitLink);
    }

    private void addAcceptButton(final Form<AbstractCRUDDTO<E>> form, final AbstractCRUDDTO<E> abstractCRUDDTO,
            final CRUDComponent<E> crudComponent) {
        final AjaxSubmitLink ajaxSubmitLink = new AjaxSubmitLink("confirmButton", form) {

            private static final long serialVersionUID = 8292048320258538553L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                feedbackModalWindow.show(target);
                abstractCRUDDTO.commit();
                crudComponent.hideForm(target);
            }

            @Override
            protected void onError(final AjaxRequestTarget target, final Form<?> form) {
                this.setResponsePage(getPage());
            }
        };
        ajaxSubmitLink.setOutputMarkupId(true);
        ajaxSubmitLink.setOutputMarkupPlaceholderTag(true);
        this.add(ajaxSubmitLink);
    }
}
